package ie.nolantech.gpsg;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class DbViewer extends ListActivity {
	
	private static final String TAG = "DbViewer";
	Cursor c;
	SimpleCursorAdapter mSca;
	TextView mTxtCount;
	
	GpsHelper mHelper;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.db_viewer);
		mHelper = new GpsHelper(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		refreshCursor();
	}
	
	private void refreshCursor(){
		mTxtCount = (TextView) findViewById(R.id.txt_count);

		

		c = mHelper.getAllLoc();

		if (c != null) {
			mTxtCount.setText("" + c.getCount());
			Log.d(TAG, "Cursor size" + c.getCount());

			startManagingCursor(c);

			mSca = new SimpleCursorAdapter(this,
					R.layout.db_row, c, new String[] { "_id", "lat", "lon",
							"date_done", "value_type" }, new int[] { R.id.txt_index,
							R.id.txt_col1, R.id.txt_col2, R.id.txt_col3, R.id.txt_value_type });

			setListAdapter(mSca);
		} else {
			Log.d(TAG, "The cursor was null");
		}
		registerForContextMenu(getListView());

	}

	
//	@Override
//	public boolean onContextItemSelected(MenuItem item) {
//		// TODO Auto-generated method stub
//		return super.onContextItemSelected(item);
//	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		//Cursor c = (Cursor) l.getItemAtPosition(position);
		//final int _id = c.getInt(c.getColumnIndex("_id"));
		final String _id= ((TextView)v.findViewById(R.id.txt_index)).getText().toString();
		final String dateDone= ((TextView)v.findViewById(R.id.txt_col3)).getText().toString();
		
		
		AlertDialog.Builder builder = new AlertDialog.Builder(DbViewer.this);
		builder.setMessage("Confirm delete[date:" + dateDone + ":id" + _id + "]")
				.setCancelable(false) 
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								GpsHelper helper = new GpsHelper(DbViewer.this);
								helper.deletePoint(Integer.parseInt(_id));
								//mSca.notifyDataSetChanged();
								//onContentChanged();
								refreshCursor();
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
		
		
		
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	protected void onPause() {
		if (c != null) {
			c.close();
		}
		super.onPause();
	}

	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Cursor c = (Cursor) l.getItemAtPosition(position);
		//final int _id = c.getInt(c.getColumnIndex("_id"));
		String lat = c.getString(c.getColumnIndex("lat"));
		String lon = c.getString(c.getColumnIndex("lon"));
		//String dateDone = c.getString(c.getColumnIndex("date_done"));
		super.onListItemClick(l, v, position, id);
		Intent i = new Intent(this, Map.class);
		i.putExtra("lat", lat);
		i.putExtra("lon", lon);
		startActivity(i);
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.db_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mnu_delete_data:
			mHelper.deleteAllLocations();
			refreshCursor();
			break;

		}
		return super.onOptionsItemSelected(item);
	}

}
